package com.meixiang.util;

import android.content.Context;

import com.meixiang.entity.JsonBean;
import com.meixiang.tool.Tool;

import org.json.JSONException;
import org.json.JSONObject;

/**
 * 虚拟数据工具类
 * 统一管理所有接口的虚拟数据
 *
 * @author LZRUI
 */
public class VirtualDataHelper {

    /**
     * 获取Result字符串
     *
     * @param context  上下文
     * @param fileName 文件名
     * @return String
     */
    private static String getResultString(Context context, String fileName) {
        String result = "";
        String json = Tool.getJsonFromAssets(context, fileName);
        try {
            JSONObject jsonObject = new JSONObject(json);
            result = jsonObject.get("result").toString();
        } catch (JSONException e) {
            e.printStackTrace();
        }
        return result;
    }

    /**
     * @param context
     * @param fileName
     * @return JsonBean
     * 获取json数据对象
     * @author yinsujun
     * created at 2016/5/24 18:03
     */
    public static JsonBean getResultData(Context context, String fileName) {
        return getResultForJsonBean(context, fileName);
    }


    private static JsonBean getResultForJsonBean(Context context, String fileName) {
        JsonBean jsonBean = new JsonBean();
        String json = Tool.getJsonFromAssets(context, fileName);
        try {
            JSONObject jsonObject = new JSONObject(json);
            JSONObject result = jsonObject.optJSONObject("result");
            jsonBean.setCode(jsonObject.optInt("code"));
            jsonBean.setMessage(jsonObject.optString("message"));
            jsonBean.setResult(jsonObject.optString("result"));
            jsonBean.setJsonResult(result);
            return jsonBean;
        } catch (JSONException e) {
            e.printStackTrace();
        }
        return jsonBean;
    }

}
